// 导入 mongoose 模块
const mongoose = require("mongoose");

// 连接数据库
mongoose.connect("mongodb://127.0.0.1:27017/bilibili");

// 回调设置
mongoose.connection.once("open", () => {
  // 创建文档结构
  let novelSchema = new mongoose.Schema({
    name: String,
    author: String,
    price: Number,
    is_hot: Boolean,
  });

  // 创建文档结构对象
  let novelModel = mongoose.model("novel", novelSchema);

  // 字段筛选
  /* novelModel
    .find()
    .select({
      _id: false,
      name: true,
      author: true,
    })
    .exec((err, data) => {
      if (err) {
        console.log(err);
        return;
      }
      console.log(data);
    }); */

  novelModel
    .find()
    .select({
      _id: 0,
      name: 1,
      price: 1,
    })
    .sort({
      price: -1,
    })
    .skip(3)
    .limit(3)
    .exec((err, data) => {
      if (err) {
        console.log(err);
        return;
      }
      console.log(data);
    });

  console.log("连接成功");
});

mongoose.connection.on("error", () => {
  console.log("连接失败");
});

mongoose.connection.on("close", () => {
  console.log("连接关闭");
});
